package com.zxc.controller.login;

import com.zxc.common.Result;
import com.zxc.model.User;
import com.zxc.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
@CrossOrigin
@RestController
@RequestMapping("/validation")
public class ValidationController {
    @Autowired
    private UserService userService;


//    @RequestMapping ( value = "/login1", method = RequestMethod.POST )
//    public Result login(@RequestBody User user) {
//        Subject currentUser = SecurityUtils.getSubject();
//        if (!currentUser.isAuthenticated()){
//            //把用户名和密码封装为UsernamepasswordToken 对象
//            UsernamePasswordToken token = new UsernamePasswordToken(user.getAccount(),user.getPassword());
//            //remember me
//            token.setRememberMe(true);
//            try{
//                //执行登陆
//                currentUser.login(token);
////这个token 到时要传到前端的
//                System.out.println("登录成功");
//                Result result = new Result();
//
//                result.setData(currentUser.getSession().getId());
//                System.out.println(result.getData());
//                return result;
//            }catch (AuthenticationException ae){
//                System.out.println("登陆失败:"+ae.getMessage());
//            }
//
//        }
//                return null;
//
//    }

    @PostMapping("/login")
    public ResponseEntity<?> getToken(HttpServletRequest request,@RequestBody User user) {
        System.out.println(user.getAccount()+"-----"+user.getPassword());
        // 验证用户信息
        Subject currentUser = SecurityUtils.getSubject();
         UsernamePasswordToken token = new UsernamePasswordToken(user.getAccount(),user.getPassword());
    //remember me
            token.setRememberMe(true);
            try{
        //执行登陆
        currentUser.login(token);
//这个token 到时要传到前端的
        System.out.println("登录成功");
        User user1= userService.getUser(user.getAccount());
                Map<String, Object> response = new HashMap<String, Object>();
                response.put("account", user1.getAccount());
                response.put("username", user1.getUserName());
                response.put("token", currentUser.getSession().getId());
                System.out.println(response);
                return ResponseEntity.ok(response);
    }catch (AuthenticationException ae){
        System.out.println("登陆失败:"+ae.getMessage());
    }
            return null;
}





    @RequestMapping ( value = "/ceshi", method = RequestMethod.POST )
    public Result ceshi(@RequestBody User user) {
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        System.out.println(session.getId());
        return null;
    }

}
